package com.dd.web.securities.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dd.web.securities.constant.ExchangeEnum;
import com.dd.web.securities.entity.Candle.CandleData;
import com.dd.web.securities.entity.IndiceLineDataItemVo;
import com.dd.web.securities.entity.IndiceLineDataVo;
import com.dd.web.securities.proxy.IndicesProxy;
import com.dd.web.securities.proxy.TodayDataProxy;
import com.dd.web.securities.service.IndicesService;
import com.mysql.cj.xdevapi.JsonArray;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

/**
 * 指数历史收盘点位服务类
 */
@Service
public class IndicesServiceImpl implements IndicesService {


    /**
     * 首页中要显示的股票代码
     */
//    @Value("${home.indices}")
    private List<Map<String,String>> homeIndicesCodes;

    public IndicesServiceImpl(){
        homeIndicesCodes = new ArrayList<>();
        Map<String,String> m1 = new HashMap<>();
        m1.put("code","000300");
        m1.put("name","沪深300");
        homeIndicesCodes.add(m1);

        Map<String,String> m2 = new HashMap<>();
        m2.put("code","000001");
        m2.put("name","上证指数");
        homeIndicesCodes.add(m2);

        Map<String,String> m3 = new HashMap<>();
        m3.put("code","000905");
        m3.put("name","中证500");
        homeIndicesCodes.add(m3);

        Map<String,String> m4 = new HashMap<>();
        m4.put("code","000016");
        m4.put("name","中证50");
        homeIndicesCodes.add(m4);
    }

    private String performance = "1个月";

    @Autowired
    private IndicesProxy indicesProxy;


    @Override
    public List<IndiceLineDataVo> queryLineDataList() {
        if (homeIndicesCodes != null){
            List<IndiceLineDataVo> lineDataVoList = new ArrayList<>();
            ResponseEntity<String> responseEntity = null;
            for (Map<String,String> item : homeIndicesCodes){
                responseEntity = indicesProxy.getIndices(item.get("code"),performance);
                IndiceLineDataVo dataVo = this.transResponse(responseEntity);
                dataVo.setTitle(item.get("name"));
                dataVo.setCode(item.get("code"));
                lineDataVoList.add(dataVo);
            }
            return lineDataVoList;
        }
        return null;
    }

    /**
     * 讲请求结果转换为线型数据对象
     * @param responseEntity
     * @return
     */
    private IndiceLineDataVo transResponse(ResponseEntity<String> responseEntity){
        if (responseEntity != null){
            IndiceLineDataVo dataVo = new IndiceLineDataVo();
            String dataStr = responseEntity.getBody();
            String tempStr = null;

            List<Map> dataList = JSONArray.parseArray(dataStr,Map.class);
            if (dataList != null){
                for (Map m : dataList){
                    IndiceLineDataItemVo item = new IndiceLineDataItemVo();
                    item.setTclose(Double.valueOf(m.get("tclose").toString()));
                    tempStr = m.get("tradedate").toString();
                    item.setTradedate(tempStr.substring(0,tempStr.indexOf(" ")));
                    dataVo.addItem(item);
                }
            }
            return dataVo;
        }

        return null;
    }

    //一天分时交易数据获取代理
    @Autowired
    private TodayDataProxy todayDataProxy;
    @Override
    public CandleData queryCandleData(ExchangeEnum exchangeEnum, String code) {
        ResponseEntity<byte[]> res = todayDataProxy.selectTodayDate(exchangeEnum.getCode()+code);
        CandleData data = this.transCandleData(res);
        return data;
    }

    /**
     * 将请求结果转换为k线图数据
     * @param res 请求结果
     * @return
     */
    private CandleData transCandleData(ResponseEntity<byte[]> res){
        String body = null ;
//        try {
            byte[] bodyByte = res.getBody();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip;
            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(bodyByte);
                gzip.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            body = new String(Base64.encodeBase64(out.toByteArray()));
            JSONObject map = JSONObject.parseObject(body);
            CandleData data = new CandleData();
            data.setCode(map.getString("symbol"));
            data.setName(map.getString("name"));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

        return null;
    }




}
